package com.dianrui.firemenmonitor.activity;

import android.app.ProgressDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.util.OkHttpUtils;
import com.dianrui.firemenmonitor.util.StringUtils;
import com.dianrui.firemenmonitor.util.ToastUtil;
import com.dianrui.firemenmonitor.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class UpdatePasswordActivity extends BaseActivity {
    private ProgressDialog mProgressDialog;


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.current_currentpwd_edit)
    EditText currentCurrentpwdEdit;
    @BindView(R.id.current_newpwd_edit)
    EditText currentNewpwdEdit;

    @Override
    public int getContentViewId() {
        return R.layout.activity_updatepwd;
    }

    @Override
    public void initView() {
        mProgressDialog = new ProgressDialog(this);
        customInit(R.color.blue_518bfa);
        title.setText(getString(R.string.update_pwd_title));
        topRight.setVisibility(View.VISIBLE);
        topRight.setText(getString(R.string.save));
    }



    @OnClick({R.id.back, R.id.top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.top_right:
                if (StringUtils.isEmpty(currentCurrentpwdEdit.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.forgetpwd_oldpwd));
                    return;
                }
                if (StringUtils.isEmpty(currentNewpwdEdit.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.forgetpwd_newpwd));
                    return;
                }
                UpdatePwd();
                break;

                default:
                    break;
        }
    }

    private void UpdatePwd() {
        mProgressDialog.setMessage("正在修改密码,请稍候...");
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("password",currentCurrentpwdEdit.getText().toString().trim());
        jsonObject.addProperty("againpass",currentNewpwdEdit.getText().toString().trim());
        OkHttpUtils.getInstance().postWithMemberToken(Url.MODIFY_PWD, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                mProgressDialog.dismiss();
                                spUtils.clear();
                                JumpLoginActivitys(LoginActivity.class);
                                ToastUtil.showToast(jsonObject.optString("msg"));
                                finish();
                            }else {
                                mProgressDialog.dismiss();
                                ToastUtil.showToast(jsonObject.optString("msg"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    mProgressDialog.dismiss();
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null &&mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
}
